/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package usercommands;

import gameserver.configs.ModsConfig;
import gameserver.model.gameobjects.player.Player;
import gameserver.services.TeleportService;
import gameserver.utils.PacketSendUtility;
import gameserver.utils.Util;
import gameserver.utils.chathandlers.UserCommand;
import gameserver.utils.i18n.CustomMessageId;
import gameserver.utils.i18n.LanguageHandler;
import gameserver.world.World;

import java.util.Map;
import javolution.util.FastMap;

public class GotoLove extends UserCommand
{
	public GotoLove()
	{
		super("gotolove");
	}
	
	private static Map<Integer, Long> lastUsage = new FastMap<Integer, Long>();
	
	@Override
	public void executeCommand(Player player, String params)
	{
		if(lastUsage.containsKey(player.getObjectId()))
		{
			if((System.currentTimeMillis() - lastUsage.get(player.getObjectId())) < ModsConfig.GOTOLOVE_COOLDOWN*1000)
			{
				PacketSendUtility.sendMessage(player, "You cannot use this command yet! Seconds left until you can use the command again:" + (ModsConfig.GOTOLOVE_COOLDOWN*1000 -(System.currentTimeMillis() - lastUsage.get(player.getObjectId())))/1000);
				return;
			}
		}
		
		if (!player.getCommonData().getMarriage())
		{
			PacketSendUtility.sendMessage(player, LanguageHandler.translate(CustomMessageId.WEDDING_NOT_MARRIED));
			return;
		}
		
		Player partner = World.getInstance().findPlayer(Util.convertName(player.getCommonData().getPartner()));

		if (partner == null)
		{
			PacketSendUtility.sendMessage(player, LanguageHandler.translate(CustomMessageId.WEDDING_LOVE_NOT_ONLINE));
			return;
		}
		// no port in jail
		if (player.isInPrison() || partner.isInPrison())
		{
			PacketSendUtility.sendMessage(player, LanguageHandler.translate(CustomMessageId.WEDDING_LOVE_NOT_PORTABLE));
			return;
		}
		// no port in instance
		if (player.isInInstance() || partner.isInInstance())
		{
			PacketSendUtility.sendMessage(player, LanguageHandler.translate(CustomMessageId.WEDDING_LOVE_NOT_PORTABLE));
			return;
		}
		// teleport-cooldown
		TeleportService.teleportTo(player, partner.getWorldId(), partner.getInstanceId(), partner.getX(), partner.getY(), partner.getZ(), partner.getHeading(), ModsConfig.GOTOLOVE_TELEPORTDELAY*1000);
		lastUsage.put(player.getObjectId(), new Long(System.currentTimeMillis()));
	}
}
